package com.engine.jucailinkq.attendance.workflow.action;

import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DateUtil;
import com.engine.jucailinkq.common.util.Utils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 补打卡数据入建模表action
 */
@Slf4j
public class MakeUpClockImportAction implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        log.debug("**********MakeUpClockImportAction import data start**********");
        // 流程表单主表数据
        HashMap<String,String> mainTableData = CommonUtil.getMainTableInfo(requestInfo);
        // 流程表单明细表数据
        List<Map<String, String>> detailTableData = CommonUtil.getDetailTableInfo(requestInfo,0);
        //补打卡人员
        String userId = mainTableData.get("bdkry");

        String modeId = Utils.getFormmodeIdMap().get("uf_jcl_kq_bdkjl");

        RecordSet rs = new RecordSet();

        String sql = "insert into uf_jcl_kq_bdkjl (bdkry,dkrq,dksj,dksm,bdklx,formmodeid,modeuuid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime) values (?,?,?,?,?,?,?,?,?,?,?)";

        List<List> lists = Lists.newArrayList();
        for (Map<String, String> detailTable:detailTableData){
            List list = Lists.newArrayList();
            list.add(userId);
            list.add(detailTable.get("bdkrq"));
            list.add(detailTable.get("bdksj"));
            list.add(detailTable.get("bdksm"));
            list.add(detailTable.get("bdklx"));
            list.add(modeId);
            list.add(UUID.randomUUID().toString());
            list.add(1);
            list.add(0);
            list.add(DateUtil.getCurrentDate());
            list.add(DateUtil.getCurrentTime().split(" ")[1]);
            lists.add(list);
        }
        boolean result = rs.executeBatchSql(sql,lists);
        if (!result){
            log.error("MakeUpClockImportAction insert data:[{}]",lists);
            log.error("添加失败");
            requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
            requestInfo.getRequestManager().setMessagecontent("添加失败！");
            return Action.FAILURE_AND_CONTINUE;
        }
        log.debug("**********MakeUpClockImportAction import data  end**********");

        return Action.SUCCESS;
    }
}
